/*
 * PratamaWijaya.com
 * set.mnemonix@gmail.com
 * Game.java
 * AndEngineGamePanjatPinang
 * 6:37:58 PM 2012
 */

package com.amikomgamedev.panjatpinang;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.Texture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.content.Context;
import android.graphics.Color;

public class Game implements Data, Define {
	private static BaseGameActivity		activity;
	private static Context				context;

	public static HUD					hud;

	// sound
	public static Music					mscBgmMenu, mscBgmGameplay,
			mscSoundKejatuhan, mscSoundVictory;

	// font
	public static Font					FntSmall;
	public static Font					FntMedium;
	public static Font					FntBig;

	// text
	public static Text					txtScore;

	private static ITexture				TexFontSmall;
	private static ITexture				TexFontMedium;
	private static ITexture				TexFontBig;

	// background gameplay
	private static BitmapTextureAtlas	TexImgGameplayBackground;
	private static ITextureRegion		TreImgGameplayBackground;
	public static Sprite				SprImgGameplayBackground;
	private static BitmapTextureAtlas 	TexImgBGUtama;
	private static ITextureRegion		TreImgBGUtama;
	public static Sprite				SprImgBGUtama;
	private static BitmapTextureAtlas 	TexImgGunung;
	private static ITextureRegion		TreImgGunung;
	public static Sprite				SprImgGunung;
	private static BitmapTextureAtlas 	TexImgBGBukitHitam;
	private static ITextureRegion		TreImgBGBukitHitam;
	public static Sprite				SprImgBGBukitHitam;
	private static BitmapTextureAtlas 	TexImgBGPohonCoklat;
	private static ITextureRegion		TreImgBGPohonCoklat;
	public static Sprite				SprImgBGPohonCoklat;
	private static BitmapTextureAtlas 	TexImgBGPohonHijau;
	private static ITextureRegion		TreImgBGPohonHijau;
	public static Sprite				SprImgBGPohonHijau;	
	private static BitmapTextureAtlas 	TexImgBGAwan1;
	private static ITextureRegion		TreImgBGAwan1;
	public static Sprite				SprImgBGAwan1;
	private static BitmapTextureAtlas 	TexImgBGAwan2;
	private static ITextureRegion		TreImgBGAwan2;
	public static Sprite				SprImgBGAwan2;
	private static BitmapTextureAtlas 	TexImgBGAwan3;
	private static ITextureRegion		TreImgBGAwan3;
	public static Sprite				SprImgBGAwan3;
	private static BitmapTextureAtlas 	TexImgBGPucuk;
	private static ITextureRegion		TreImgBGPucuk;
	public static Sprite				SprImgBGPucuk;
	
	// loading background
	private static BitmapTextureAtlas	TexImgLoadingBg;
	private static ITextureRegion		TreImgLoadingBg;
	public static Sprite				SprImgLoadingBg;

	// button gameplay
	private static BitmapTextureAtlas	TexImgButtonGameplay, TexImgBtnRestart;
	private static ITextureRegion		TreImgBtnPanjat, TreImgBtnSwitch,
			TreImgBtnRestart;
	public static Sprite				SprBtnPanjat, SprBtnSwitch,
			SprBtnRestart;

	// interface gameplay
	private static BitmapTextureAtlas	TexImgPinang, TexImgTimer,
			TexImgSukses, TexImgGagal;
	private static ITextureRegion		TreImgPinang, TreImgTimer,
			TreImgSukses, TreImgGagal;
	public static Sprite				SprPinang, SprTimer, SprSukses,
			SprGagal;

	// indikator
	private static BitmapTextureAtlas	TexIndikator;
	private static ITextureRegion		TreIndikatorGaris, TreIndikatorPoint;
	public static Sprite				SprIndikatorGaris, SprIndikatorPoint;

	// main char
	private static BitmapTextureAtlas	TexMainchar, TexMaincharLompat;
	public static TiledTextureRegion	TleMainchar, TleMaincharLompat;
	public static AnimatedSprite		AspMainchar, AspMaincharLompat;
	public static AnimatedSprite		FlipAspMainChar;

	// item bendaJatuh
	private static BitmapTextureAtlas	TexItemBendaJatuh;
	private static ITextureRegion		TreItemEmber, TreItemKerdus,
			TreItemMagicjar, TreItemPayung, TreItemTas;
	public static Sprite				sprEmber, sprKerdus, sprMagicjar,
			sprPayung, sprTas;

	public static void setContext(Context _context) {
		context = _context;
		activity = (BaseGameActivity) _context;
	}

	public static void loadTexture(Texture texture) {
		activity.getEngine().getTextureManager().loadTexture(texture);
	}

	public static void loadFont(Font font) {
		activity.getEngine().getFontManager().loadFont(font);
	}

	public static void loadBgLoading() {
		Utils.TRACE("load : loading bg");
		if (TexImgLoadingBg != null)
			return;

		TexImgLoadingBg = new BitmapTextureAtlas(activity.getTextureManager(),
				IMG_BG_LOADING_TEX_WIDTH, IMG_BG_LOADING_TEX_HEIGHT);
		TreImgLoadingBg = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexImgLoadingBg, activity.getAssets(),
						IMG_LOADING_BG, 0, 0);
		loadTexture(TexImgLoadingBg);

		SprImgLoadingBg = new Sprite(0, 0, TreImgLoadingBg,
				activity.getVertexBufferObjectManager());

	}

	public static void loadBgmMenu() {
		Utils.TRACE("load music menu");
		try {
			mscBgmMenu = MusicFactory.createMusicFromAsset(
					activity.getMusicManager(), context, SND_BGM_MENU);
			mscBgmMenu.setLooping(true);
		} catch (Exception e) {
			// TODO: handle exception
			Utils.TRACE(e);
		}

	}

	public static void loadBgmGameplay() {
		Utils.TRACE("load music gameplay");
		try {
			mscBgmGameplay = MusicFactory.createMusicFromAsset(
					activity.getMusicManager(), context, SND_BGM_GAMEPLAY);
			mscBgmGameplay.setLooping(true);
			mscSoundKejatuhan = MusicFactory.createMusicFromAsset(
					activity.getMusicManager(), context, SND_SOUND_KEJATUHAN);
			mscSoundKejatuhan.setLooping(false);
			mscSoundVictory = MusicFactory.createMusicFromAsset(
					activity.getMusicManager(), context, SND_SOUND_VICTORY);
			mscSoundVictory.setLooping(false);
		} catch (Exception e) {
			// TODO: handle exception
			Utils.TRACE(e);
		}
	}

	public static void loadFontSmall() {
		Utils.TRACE("load font small");
		if (TexFontSmall != null)
			return;
		TexFontSmall = new BitmapTextureAtlas(activity.getTextureManager(),
				FNT_TEX_WIDTH, FNT_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		FntSmall = FontFactory.createFromAsset(activity.getFontManager(),
				TexFontSmall, activity.getAssets(), Data.FONT_STYLE,
				Data.FONT_SIZE_SMALL, true, Color.WHITE);

		loadFont(FntSmall);

	}

	public static void loadFontMedium() {
		Utils.TRACE("load font medium");
		TexFontMedium = new BitmapTextureAtlas(activity.getTextureManager(),
				FNT_TEX_WIDTH, FNT_TEX_HEIGHT, TextureOptions.BILINEAR);
		FntMedium = FontFactory.createFromAsset(activity.getEngine()
				.getFontManager(), TexFontMedium, activity.getAssets(),
				FONT_STYLE, FONT_SIZE_MEDIUM, true, Color.BLACK);
		loadFont(FntMedium);
	}

	public static void loadFontBig() {
		Utils.TRACE("load font Big");
		TexFontBig = new BitmapTextureAtlas(activity.getTextureManager(),
				FNT_TEX_WIDTH, FNT_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		FntBig = FontFactory.createFromAsset(activity.getFontManager(),
				TexFontBig, activity.getAssets(), Data.FONT_STYLE,
				Data.FONT_SIZE_BIG, true, Color.WHITE);
		loadFont(FntBig);
	}

	public static void loadGameplayBg() {
		Utils.TRACE("load gameplay background");
		// if (TexImgGameplayBackground != null)
		// return;
		TexImgGameplayBackground = new BitmapTextureAtlas(
				activity.getTextureManager(), IMG_BG_GAMEPLAY_TEX_WIDTH,
				IMG_BG_GAMEPLAY_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgGameplayBackground = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexImgGameplayBackground,
						activity.getAssets(), Data.IMG_BG_GAMEPLAY_UTAMA, 0, 0);

		TreImgGunung = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexImgGameplayBackground,
						activity.getAssets(), Data.IMG_BG_GUNUNG, 320, 0);
		
		TexImgBGUtama = new BitmapTextureAtlas(
				activity.getTextureManager(), IMG_BG_GAMEPLAY_LATAR_WIDTH,
				IMG_BG_GAMEPLAY_LATAR_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgBGUtama = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexImgBGUtama,
						activity.getAssets(), Data.IMG_BG_LATAR, 0, 0);
		
		loadTexture(TexImgGameplayBackground);
		loadTexture(TexImgBGUtama);

		SprImgGameplayBackground = new Sprite(0, 0, TreImgGameplayBackground,
				activity.getEngine().getVertexBufferObjectManager());
		SprImgGunung = new Sprite(0, 0, TreImgGunung,
				activity.getEngine().getVertexBufferObjectManager());
		SprImgBGUtama = new Sprite(0, 0, TreImgBGUtama,
				activity.getEngine().getVertexBufferObjectManager());

	}

	public static void loadInterfaceGameplay() {
		Utils.TRACE("load interface gameplay");
		// load button panjat sama swap
		TexImgButtonGameplay = new BitmapTextureAtlas(
				activity.getTextureManager(), BTN_GAMEPLAY_TEX_WIDTH,
				BTN_GAMEPLAY_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgBtnPanjat = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexImgButtonGameplay, activity.getAssets(),
						IMG_BTN_PANJAT, 0, 0);
		TreImgBtnSwitch = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexImgButtonGameplay, activity.getAssets(),
						IMG_BTN_SWITCH, 80, 0);

		TexImgBtnRestart = new BitmapTextureAtlas(activity.getTextureManager(),
				IMG_BTN_RESTART_WIDTH, IMG_BTN_RESTART_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgBtnRestart = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexImgBtnRestart, activity.getAssets(),
						IMG_BTN_RESTART, 0, 0);

		TexImgPinang = new BitmapTextureAtlas(activity.getTextureManager(),
				IMG_PINANG_TEX_WIDTH, IMG_PINANG_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgPinang = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexImgPinang, activity.getAssets(), IMG_PINANG, 0, 0);

		TexImgTimer = new BitmapTextureAtlas(activity.getTextureManager(), 512,
				512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreImgTimer = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexImgTimer, activity.getAssets(), IMG_TIMER, 0, 0);

		// interface indikator
		TexIndikator = new BitmapTextureAtlas(activity.getTextureManager(),
				IMG_PINANG_TEX_WIDTH, INDIKATOR_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TreIndikatorGaris = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexIndikator, activity.getAssets(),
						IMG_INDIKATOR_INTERFACE, 0, 0);
		TreIndikatorPoint = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexIndikator, activity.getAssets(),
						IMG_INDIKATOR_POINT, 48, 0);

		// load MC sukses gagal
		TexImgSukses = new BitmapTextureAtlas(activity.getTextureManager(),
				IMG_SUKSES_WIDTH, IMG_SUKSES_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TexImgGagal = new BitmapTextureAtlas(activity.getTextureManager(),
				IMG_SUKSES_WIDTH, IMG_SUKSES_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		TreImgSukses = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexImgSukses, activity.getAssets(), IMG_MC_SUKSES, 0, 0);
		TreImgGagal = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexImgGagal, activity.getAssets(), IMG_MC_GAGAL, 0, 0);

		// load all texture
		loadTexture(TexImgButtonGameplay);
		loadTexture(TexImgPinang);
		loadTexture(TexIndikator);
		loadTexture(TexImgTimer);
		loadTexture(TexImgSukses);
		loadTexture(TexImgGagal);
		loadTexture(TexImgBtnRestart);

		// pasang ke sprite

		// pinang
		SprPinang = new Sprite(0, 0, TreImgPinang,
				activity.getVertexBufferObjectManager());

		// button
		SprBtnSwitch = new Sprite(BUTTON_SWITCH_POS[0], BUTTON_SWITCH_POS[1],
				TreImgBtnSwitch, activity.getVertexBufferObjectManager());

		SprBtnPanjat = new Sprite(BUTTON_PANJAT_POS[0], BUTTON_PANJAT_POS[1],
				TreImgBtnPanjat, activity.getEngine()
						.getVertexBufferObjectManager());

		SprBtnRestart = new Sprite(5, 25, TreImgBtnRestart, activity.getEngine()
				.getVertexBufferObjectManager());

		// indikator
		SprIndikatorGaris = new Sprite(10, 100, TreIndikatorGaris, activity
				.getEngine().getVertexBufferObjectManager());
		SprIndikatorPoint = new Sprite(0, 0, TreIndikatorPoint, activity
				.getEngine().getVertexBufferObjectManager());
		SprIndikatorPoint.setPosition(SprIndikatorGaris.getX()
				+ (SprIndikatorPoint.getWidth() / 2) + 1,
				SprIndikatorGaris.getY() - 8 + SprIndikatorGaris.getHeight());
		SprTimer = new Sprite(0, 0, TreImgTimer, activity.getEngine()
				.getVertexBufferObjectManager());

		// MC gagal sukses
		SprSukses = new Sprite(0, 0, TreImgSukses, activity.getEngine()
				.getVertexBufferObjectManager());
		SprGagal = new Sprite(0, 0, TreImgGagal, activity.getEngine()
				.getVertexBufferObjectManager());

	}

	public static void loadBendaJatuh() {
		TexItemBendaJatuh = new BitmapTextureAtlas(
				activity.getTextureManager(), ITEM_TEX_WIDTH, ITEM_TEX_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		TreItemEmber = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexItemBendaJatuh, activity.getAssets(), IMG_ITEM_EMBER, 0, 0);
		TreItemKerdus = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexItemBendaJatuh, activity.getAssets(),
						IMG_ITEM_KERDUS, 60, 0);
		TreItemMagicjar = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(TexItemBendaJatuh, activity.getAssets(),
						IMG_ITEM_MAGICJAR, 150, 0);
		TreItemPayung = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexItemBendaJatuh, activity.getAssets(), IMG_ITEM_PAYUNG, 214,
				0);
		TreItemTas = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				TexItemBendaJatuh, activity.getAssets(), IMG_ITEM_TAS, 288, 0);

		loadTexture(TexItemBendaJatuh);
		sprEmber = new Sprite(0, 0, TreItemEmber,
				activity.getVertexBufferObjectManager());
		sprKerdus = new Sprite(0, 0, TreItemKerdus,
				activity.getVertexBufferObjectManager());
		sprMagicjar = new Sprite(0, 0, TreItemMagicjar,
				activity.getVertexBufferObjectManager());
		sprPayung = new Sprite(0, 0, TreItemPayung,
				activity.getVertexBufferObjectManager());
		sprTas = new Sprite(0, 0, TreItemTas,
				activity.getVertexBufferObjectManager());

	}

	public static void loadMainchar() {

		TexMainchar = new BitmapTextureAtlas(activity.getTextureManager(),
				SPR_MAIN_CHAR_WIDTH, SPR_MAIN_CHAR_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		TleMainchar = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(TexMainchar, activity.getAssets(),
						SPR_MAIN_CHAR, 0, 0, SPR_MC_COLUMN, SPR_MC_ROW);

		loadTexture(TexMainchar);

		AspMainchar = new AnimatedSprite(0, 0, TleMainchar,
				activity.getVertexBufferObjectManager());
		FlipAspMainChar = new AnimatedSprite(0, 0, TleMainchar,
				activity.getVertexBufferObjectManager());

		FlipAspMainChar.setFlippedHorizontal(true);
	}
}
